package com.example.myys.viewModel.syModel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.myys.data.entity.DataBean
import com.example.myys.network.parse.SyParse
import com.example.myys.ui.Resource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class SyViewModel : ViewModel() {
    val syParse = SyParse()

    //电影
    private val _ysState: MutableStateFlow<Resource<List<DataBean>>> =
        MutableStateFlow(value = Resource.Loading())

    val ysState: StateFlow<Resource<List<DataBean>>>
        get() = _ysState

    //电视剧
    private val _dsjState: MutableStateFlow<Resource<List<DataBean>>> =
        MutableStateFlow(value = Resource.Loading())

    val dsjState: StateFlow<Resource<List<DataBean>>>
        get() = _dsjState

    //综艺
    private val _zyState: MutableStateFlow<Resource<List<DataBean>>> =
        MutableStateFlow(value = Resource.Loading())

    val zyState: StateFlow<Resource<List<DataBean>>>
        get() = _zyState

    //动漫
    private val _dmState: MutableStateFlow<Resource<List<DataBean>>> =
        MutableStateFlow(value = Resource.Loading())

    val dmState: StateFlow<Resource<List<DataBean>>>
        get() = _dmState

    //短剧
    private val _djState: MutableStateFlow<Resource<List<DataBean>>> =
        MutableStateFlow(value = Resource.Loading())

    val djState: StateFlow<Resource<List<DataBean>>>
        get() = _djState

    //小说
    private val _xsState: MutableStateFlow<Resource<List<DataBean>>> =
        MutableStateFlow(value = Resource.Loading())

    val xsState: StateFlow<Resource<List<DataBean>>>
        get() = _xsState

    fun getPh() {
        viewModelScope.launch(Dispatchers.IO) {
            val ysList = syParse.getYsPh()
            if (ysList == null) {
                _ysState.value = Resource.Error()
            } else {
                _ysState.value = Resource.Success(ysList)
            }
            val dsjList = syParse.getDsjPh()
            if (dsjList == null) {
                _dsjState.value = Resource.Error()
            } else {
                _dsjState.value = Resource.Success(dsjList)
            }
            val zyList = syParse.getZyPh()
            if (zyList == null) {
                _zyState.value = Resource.Error()
            } else {
                _zyState.value = Resource.Success(zyList)
            }
            val dmList = syParse.getDmPh()
            if (dmList == null) {
                _dmState.value = Resource.Error()
            } else {
                _dmState.value = Resource.Success(dmList)
            }
            val djList = syParse.getDjPh()
            if (djList == null) {
                _djState.value = Resource.Error()
            } else {
                _djState.value = Resource.Success(djList)
            }
            val xsList = syParse.getXsPh()
            if (xsList == null) {
                _xsState.value = Resource.Error()
            } else {
                _xsState.value = Resource.Success(xsList)
            }


        }
    }

    init {
        getPh()
    }
}